<?php

// phpMyRealty 3
//
// File Name: defaults.php
// File Location : ./
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

if (!defined('PMR') || (defined('PMR') && PMR != 'true')) die();

// Enable complete error reporting
// inluding Notices and Warnings
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Disable magic_quotes_runtime
set_magic_quotes_runtime (0);

// Include the main functions file
include ( PATH . '/includes/functions.php' );

// Include the database class
include ( PATH . '/includes/functions/db.php' );

// Include the sessions class file
include ( PATH . '/includes/functions/sessions.php' );

// Include zip code radius search
if ($encoder == 'Z')
 include ( PATH . '/includes/functions/Zend/zip.php' );
else
 include ( PATH . '/includes/functions/ionCube/zip.php' );

// Include package functions
if ($encoder == 'Z')
 include ( PATH . '/includes/functions/Zend/packages.php' );
else
 include ( PATH . '/includes/functions/ionCube/packages.php' );

// Include the phpMailer class
include ( PATH . '/includes/phpmailer/class.phpmailer.php' );

// Include the template class
include ( PATH . '/includes/functions/template.php' );

// Include the captcha class
include ( PATH . '/includes/functions/captcha.php' );

// Include the favorites class
include ( PATH . '/includes/functions/favorites.php' );

// Include the html filter class
include ( PATH . '/includes/functions/filter.php' );

// Include the html form class
include ( PATH . '/includes/functions/form.php' );

// Include the images class
include ( PATH . '/includes/functions/images.php' );

// Include the ratings class
include ( PATH . '/includes/functions/ratings.php' );

// Include the system class
include ( PATH . '/includes/functions/system.php' );

// Include the graph class
if ($encoder == 'Z')
 include ( PATH . '/includes/functions/Zend/graph.php' );
else
 include ( PATH . '/includes/functions/ionCube/graph.php' );

// Include the authentication class
if ($encoder == 'Z')
 include ( PATH . '/includes/functions/Zend/auth.php' );
else
 include ( PATH . '/includes/functions/ionCube/auth.php' );

// Include the user modifications
include ( PATH . '/modules/modules.php' );

// Initialise a new user session or start an existing one
$session = new Session();

// Allow Back button in IE
header("Cache-control: private");

// Start a new sql connection
$db = new Dbaccess();

if (!$db->connect ( $dbi['sql_host'], $dbi['sql_username'], $dbi['sql_password'], $dbi['sql_dbname'] ) ) {
 if (mysql_error() == '') 
  $database_incorrect = 'Database name is incorrect or doesn\'t exist'; 
 else 
  $database_incorrect = '';
 error('Initialization Error', mysql_error () . $database_incorrect );
}

// Read configuration from the database
$sql = 'SELECT name, val FROM ' . CONFIGURATION_TABLE;
$r = $db->query ($sql) or error ( 'Continue Installation', 'Database tables do not exist. Please run the installer to complete the installation process: <a href="' . URL . '/install/install.php">' . URL . '/install/install.php</a>' );
$conf = array ();

while ( $conf_array = $db->fetcharray ($r) ) {
 $conf[$conf_array['name']] = $conf_array['val'];
}

// Template / Language selection via options.php

// If template cookie exist we use it
if ( isset ( $_SESSION['intemplate'] ) ) {
 $cookie_template = $_SESSION['intemplate'];
}

// If template was changed in the options.php form we create/update
// user cookie
if ( isset ( $_POST['option_template'] ) ) {
 $session->set('intemplate', $_POST["option_template"]);
 $cookie_template = $_POST["option_template"];
}

// If template was never changed in the options.php form
// and the 'template' cookie doesn't exist we use the
// default template or the one set in the
// configuration file.
if ( !isset ( $_POST['option_template'] ) && !isset ( $_SESSION['intemplate'] ) ) {
 if ( !empty ( $conf['template'] ) ) {
  $session->set('intemplate', $conf['template']);
  $cookie_template = $conf['template'];
 }
 else {
  $session->set('intemplate', 'default');
  $cookie_template = 'default';
 }
}  

// If language cookie exist we use it
if ( isset ( $_SESSION['language'] ) ) {
 $cookie_language = $_SESSION['language'];
}

// If language was changed in the options.php form we create/update
// user cookie
if ( isset ( $_POST['option_language'] ) ) {
 $session->set('language', $_POST["option_language"]);
 $cookie_language = $_POST["option_language"];
}

// If language was never changed in the options.php form
// and the 'language' cookie doesn't exist we use the
// default language (english) or the one set in the
// configuration file.
if ( !isset ( $_POST['option_language'] ) && !isset ( $_SESSION['language'] ) ) {
 if ( !empty ( $conf['language'] ) ) {
  $session->set('language', $conf['language']);
  $cookie_language = $conf['language'];
 }
 else {
  $session->set('language', 'english');
  $cookie_language = 'english';
 }
}  

if ( isset ( $_GET['lang'] ) ) {
 $session->set('language', $_GET["lang"]);
 $cookie_language = $_GET["lang"];
}

if ( isset ( $_GET['template'] ) ) {
 $session->set('intemplate', $_GET["template"]);
 $cookie_template = $_GET["template"];
}

// Include language file containging in $cookie_language
$lang = array();
include ( PATH . '/languages/' . $cookie_language . '.lng.php' );

// Clean the Favorite Listings Cookie
favoriteListingsClean();

// Clean the Visited Listings Cookie
visitedListingsClean();

// Replace \' to ' if server recodes POST or GET variables
function replacer ( $input ) { $text = str_replace("\\'", "'", $input); return $text; }

$_POST = array_map('replacer', $_POST);

include ( PATH . '/languages/settings.php' );

?>